Ext.namespace("GridNetworkResource");

GridNetworkResource.List=function(){
	
	this.bsDetailPanel = new GridBaseStation.Detail(this);
	this.bsRepeaterDetailPanel = new GridBsRepeater.Detail(this);
	this.fiberInfoDetailPanel = new GridFiberInfo.Detail(this);
	
	this.txtNrName = new Ext.form.TextField({
		width:100
	});
	
	this.cmbNrRegion = new Ext.form.ComboBox({
		width :120,
		store : new Ext.data.JsonStore({
		   url:"../gridareas/listAllRegions.form",
		   autoLoad : true,
		   root:"regions",
		   fields:["areaId","name"]
		}),
		valueField : 'areaId',
		displayField :'name',
        mode: 'local',
//		typeAhead :true,
		triggerAction :'all',
		emptyText :'请选择所在区域',
		selectOnFocus :true	
	});
	
	this.cmbNrType = new Ext.form.ComboBox({
        width: 120,
        store: OMS.NETWORK_RESOURCE_TYPE,
        typeAhead:true,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'请选择资源类别',
        selectOnFocus: true
    });
    
     this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查询',
        tooltip: {text: '查询网络资源'},
        handler: this.onSearch,
        scope: this
    });
    
    this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../gridnetworkresource/list.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'networkResources',
			totalProperty: 'total',
			id: 'nrId',
			fields: ['nrId','nrName','nrAddressDetail','nrType','locationDesc']
		}),
		listeners:{
			 beforeload:{
				fn:function(thiz,options){
					Ext.apply(thiz.baseParams,{
						name:this.txtNrName.getValue(),
						partition:this.cmbNrRegion.getValue(),
						type:this.cmbNrType.getValue()
					});
				},
				scope:this
			}
		}
	});
	
	this.colModel = new Ext.grid.ColumnModel({
		defaults:{
			width:40,
			sortable:true,
			align:'center'
		},
		columns:[
			new Ext.grid.RowNumberer(),
			{id:'nrId',header:'编号',dataIndex:'nrId',hidden:true},
			{header:'资源名称',dataIndex:'nrName',width:80},
			{header:'资源地址',dataIndex:'nrAddressDetail',width:100},
			{header:'资源类别',dataIndex:'nrType',renderer:this.renderTypes,width:70},
			{header:'位置描述',dataIndex:'locationDesc',width:120}
		]
	});
	
	GridNetworkResource.List.superclass.constructor.call(this,{
		title: '网络资源列表',
		region: 'center',
		tbar: ['资源名称:',this.txtNrName,//'所在区域:',this.cmbNrRegion,
	                		   '资源类别:',this.cmbNrType],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		loadMask: {msg:'加载中，请稍候...'},
		bbar:new Ext.PagingToolbar({
			pageSize:15,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条网格信息，一共 {2} 条',
			emptyMsg: '当前没有记录'
		}),
		viewConfig:{forceFit:true},
		listeners: {
            render: function() {
				new Ext.Toolbar({
					renderTo: this.tbar,
					items: [this.btnSearch, '-',
							{text:'增加基站', scope:this, iconCls:'icon-add', handler:this.addBaseStation},
							//{text:'增加直放站', scope:this, iconCls:'icon-add', handler:this.addBsRepeater},
							//{text:'增加光纤', scope:this, iconCls:'icon-add', handler:this.addFiberInfo},
							{text:'修改', scope:this, iconCls:'icon-modify', handler:this.onModify},
						    {text:'删除', scope:this, iconCls:'icon-delete', handler:this.onDelete}]
				});
			}
		}
	});
	this.on('dblclick',this.onModify,this);
	this.store.load();	
};

Ext.extend(GridNetworkResource.List,Ext.grid.GridPanel,{
	onSearch:function(){
		this.store.load({
			params:{
				start:0,
				limit:15,
				name:this.txtNrName.getValue(),
				regionId:this.cmbNrRegion.getValue(),
				type:this.cmbNrType.getValue()
			}
		});
	},
	renderTypes: function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.NETWORK_RESOURCE_TYPE[parseInt(value)-1][1];
		}
		return '';
	},

	update:function(){
		this.store.reload();
	},
	addBaseStation: function(btn, event) {
		if (this.bsDetailPanel == null) {
			this.bsDetailPanel = new GridBaseStation.Detail(this);
		}
		this.bsDetailPanel.show();
		this.bsDetailPanel.initData();
	},
	addBsRepeater: function(btn, event) {
		if (this.bsRepeaterDetailPanel == null) {
			this.bsRepeaterDetailPanel = new GridBsRepeater.Detail(this);
		}
		this.bsRepeaterDetailPanel.show();
		this.bsRepeaterDetailPanel.initData();
	},
	addFiberInfo: function(btn, event) {
		if (this.fiberInfoDetailPanel == null) {
			this.fiberInfoDetailPanel = new GridFiberInfo.Detail(this);
		}
		this.fiberInfoDetailPanel.show();
		this.fiberInfoDetailPanel.initData();
	},
	onDelete: function(btn, event) {		
		var connDelete = new Ext.data.Connection({
			url: '../gridnetworkresource/delete.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		});
		
		var record = this.getSelectionModel().getSelected();
		if(record!=null){
			//Ext.MessageBox.confirm("提示","是否确认删除？");
			Ext.MessageBox.confirm("提示","确认删除  '"+record.data.nrName+"' ？",function(btn){
            if(btn=="yes"){
                //alert("yes");
                Utils.request(connDelete, {
					params: {
						'nrId': record.data.nrId
					},
					scope: this,
					success: function(resp, action) {
						//this.scope.update();
						this.scope.store.load();
						//this.scope.hide();
					},
					after: function() {}
				});
            }   
            else{
               }
			},this);
			
		}else{		
			Ext.MessageBox.alert('提示','请先选中一条记录!');
		}
	},
	onModify: function(btn, event) {		
		var record = this.getSelectionModel().getSelected();
		if(record!=null){
			var nrType = record.data.nrType;
			if(nrType == '1'){
				if (this.bsDetailPanel == null) {
					this.bsDetailPanel = new GridBaseStation.Detail(this);
				}
				this.bsDetailPanel.show();
				this.bsDetailPanel.initData(record.data.nrId);
			}else if(nrType == '2'){
				if (this.bsRepeaterDetailPanel == null) {
					this.bsRepeaterDetailPanel = new GridBsRepeater.Detail(this);
				}
				this.bsRepeaterDetailPanel.show();
				this.bsRepeaterDetailPanel.initData(record.data.nrId);
			}else if(nrType == '3'){
				if (this.fiberInfoDetailPanel == null) {
					this.fiberInfoDetailPanel = new GridFiberInfo.Detail(this);
				}
				this.fiberInfoDetailPanel.show();
				this.fiberInfoDetailPanel.initData(record.data.nrId);
			}
		}
	}
});


var lister = null;
GridNetworkResource.main=function(){
	lister = new GridNetworkResource.List();
	var mainview = new Ext.Viewport({
		layout:'border',
		items:[lister]
	});
};

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget='side';
	Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
	GridNetworkResource.main();
});

function initGisData(name, iconId, coordx, coordy){
	if(lister.bsDetailPanel.isVisible()){
		lister.bsDetailPanel.initGis(name, iconId, coordx, coordy);	
	}
	if(lister.bsRepeaterDetailPanel.isVisible()){
		lister.bsRepeaterDetailPanel.initGis(name, iconId, coordx, coordy);	
	}
};

function initGisLineData(lineColor, lineWidth, lineStyle, lineText,coordInfo){
	lister.fiberInfoDetailPanel.initGis(lineColor, lineWidth, lineStyle, lineText,coordInfo);
};
